local Header = ShortKey.Header
local UserConfig = ShortKey.UserConfig
local InnerConfig = ShortKey.InnerConfig
local Setting = ShortKey.Setting
local DataAccess = ShortKey.DataAccess

local Setting = ShortKey.Setting
local OutsideConfig = ShortKey.OutsideConfig
local InsideConfig = ShortKey.InsideConfig

local LoadState = false;

------------------------------------------------------------------------------------------------------
--ShortKey Service
------------------------------------------------------------------------------------------------------
ShortKey.Config = ShortKey.OutsideConfig

function ShortKey.Init()
	BINDING_HEADER_ShortKeyTitle = "ShortKey By simonw. http://cwowaddon.com"
	BINDING_NAME_ShortKey1 = "Reload UI"
	--BINDING_NAME_ShortKey2 = "List Binding Key"
	print("ShortKey installed. By simonw from CWDG cwowaddon.com")

	DataAccess:Load()

	if (ShortKey.Setting.IsAutoLoad) then
		ShortKey.Load()
	else
		print("ShortKey is set to load by manual, you need to use command [/sk load] to load it.")
	end

	ShortKey.ShowState()
end

function ShortKey.SaveSetting()
	DataAccess:Save()
end

function ShortKey.ResetSetting()
	DataAccess:ResetSetting()
	print("ShortKey setting is cleared, you need to reload UI.")
end

function ShortKey.Load()
	if (not LoadState) then
		LoadState = true

		if (Setting.IsAutoSelect) then
			UserConfig:LoadAuto(ShortKey.OutsideConfig)
			UserConfig:LoadAuto(ShortKey.InsideConfig)
		else
			UserConfig:LoadManual(ShortKey.OutsideConfig)
			UserConfig:LoadManual(ShortKey.InsideConfig)
		end

		InnerConfig:Import(UserConfig)

		--non secure environment
		Header:Create()
		Header:Fill()
		--secure environment
		Header:Execute()
	else
		print("ShortKey has been already loaded, you can't load it again until reload UI.")
	end
end

function ShortKey.ShowState()
	local msg = string.format("ShortKey Info: %s AddonLoaded: %s LoadedOutsideGroup: %s  LoadedInsideGroup: %s",
		ShortKey.Setting:ToString(),
		tostring(LoadState),
		ShortKey.OutsideConfig:ToString(),
		ShortKey.InsideConfig:ToString()
	)

	print(msg)
end

function ShortKey.ShowList()
	for _, useritem in ipairs(UserConfig.Items) do
		print(UserConfig:ToString(useritem))
	end
end

function ShortKey.DoSetting(cmd)
	if (cmd) then
		if (cmd == "autoload") then
			ShortKey.Setting.IsAutoLoad = not ShortKey.Setting.IsAutoLoad
			ShortKey.SaveSetting()
			print(ShortKey.Setting:ToString())

		elseif (cmd == "autoselect") then
			ShortKey.Setting.IsAutoSelect = not ShortKey.Setting.IsAutoSelect
			ShortKey.SaveSetting()
			print(ShortKey.Setting:ToString())

		elseif (cmd == "clear") then
			ShortKey.ClearSetting()

		elseif (cmd == "list") then
			ShortKey.ShowList()

		elseif (cmd == "load") then
			ShortKey.Load()
		end
	else
		ShortKey.ShowState()
	end
end